/**

 @Name：layuiAdmin 主页控制台
 @Author：贤心
 @Site：
 @License：GPL-2
    
 */


layui.define(function(exports){
  
  /*
    下面通过 layui.use 分段加载不同的模块，实现不同区域的同时渲染，从而保证视图的快速呈现
  */
  
  
  //区块轮播切换
  layui.use(['admin', 'carousel'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,carousel = layui.carousel
    ,element = layui.element
    ,device = layui.device();

    //轮播切换
    $('.layadmin-carousel').each(function(){
      var othis = $(this);
      carousel.render({
        elem: this
        ,width: '100%'
        ,arrow: 'none'
        ,interval: othis.data('interval')
        ,autoplay: othis.data('autoplay') === true
        ,trigger: (device.ios || device.android) ? 'click' : 'hover'
        ,anim: othis.data('anim')
      });
    });
    
    element.render('progress');
    
  });
  //数据概览
  layui.use(['carousel', 'echarts'], function(){
    var $ = layui.$
    ,carousel = layui.carousel
    ,echarts = layui.echarts;
	var pdate=[];
	var data1=[];
	var data2=[];
	var maxmun;
	$.ajax({
				url:"../../../Message/Message",
				type:"post",
				datatype:"json",
				success:function(res){
						
						for(var i =0 ; i < res.data.length;i++){
						var att={name:res.data[i].name,value:res.data[i].jshouji};
					
						data2.push(att)				
				}			
					
				}
			});
	 $.ajax({
			url:"../../../Message/collectNum",
			type:"post",
			datatype:"json",
			success:function(res){
										
			
					for(var i =0 ; i < res.data.length;i++){
						var att={name:res.data[i].name,value:res.data[i].jshouji};				
						
						pdate.push(res.data[i].pdate);
						data1.push(res.data[i].data11);
						maxmun=Math.max.apply(null,data1);
					}
		
			
    var echartsApp = [], options = [       
      //各类废弃物收集量分布
      {
			title : {
				text: '近30天收集量趋势图',
				x:'right',
				y:'top',
				textStyle: {
				fontSize: 25,
				fontWeight:'bold',
				color:'#19A094'
				}
			},
		
			animation:true,
			tooltip: {      
				axisPointer: {
					type: 'cross',
					crossStyle: {
						color: '#999'
					}
				},
				formatter:"{a} <br/>{b} : {c} 箱 "
			},
			xAxis: [
				{
					type: 'category',
					data: pdate,
					axisPointer: {
						type: 'shadow'
					},
					axisTick: {
						alignWithLabel: true
					},
				}
			],
			yAxis: [
				{
					type: 'value',
					name: '收集量(单位：箱)',
					min: 0,
					nameTextStyle:{
						fontSize:15
						
					},
					interval: 50,
					axisLabel: {
						formatter: '{value} '
						
					}
				}
			],
			series: [
				{
					name:'收集总量',
					type:'bar',
					data:data1,
					itemStyle: {
						normal: {
							label:{
							show: true,
							position: 'top'
							}
						}
					},
				},
				{
					name:'收集总量',
					type:'line',
					smooth:true,  
					data:data1
				}
			]
 
		},{ 
        title : {
          text: '本日各类废弃物收集量分布',
           x:'right',
			y:'top',
          textStyle: {
            fontSize: 25,
			fontWeight:'bold',
			color:'#19A094'
          }
        },
		
        tooltip : {
          trigger: 'item',
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
		color:['#39B8D8','#859DAD','#FFB5C5','#EE9572','#F5DEB3'],
        legend: {
          orient : 'vertical',
          x : 'left',
          data:['感染性废物','损伤性废物','病理性废物','药物性废物','化学性废物']
        },
        series : [{
          name:'医疗废物分布',
          type:'pie',
          radius : '80%',
          center: ['50%', '50%'],
          data:data2
        }]
      }
    ]
    ,elemDataView = $('#LAY-index-dataview').children('div')
    ,renderDataView = function(index){
      echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
      echartsApp[index].setOption(options[index]);
      window.onresize = echartsApp[index].resize;
    };
    
    
    //没找到DOM，终止执行
    if(!elemDataView[0]) return;
    
    
    
    renderDataView(0);
    
    //监听数据概览轮播
    var carouselIndex = 0;
    carousel.on('change(LAY-index-dataview)', function(obj){
      renderDataView(carouselIndex = obj.index);
    });
    
    //监听侧边伸缩
    layui.admin.on('side', function(){
      setTimeout(function(){
        renderDataView(carouselIndex);
      }, 300);
    });
    
    //监听路由
    layui.admin.on('hash(tab)', function(){
      layui.router().path.join('') || renderDataView(carouselIndex);
    });
	}
	});		
  });  
  exports('console', {})
});